package com.digitalchina.lxnsy.activity.old;

import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessTechnology;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.TechnologyVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.AnswerQuestionListHolder;
import com.digitalchina.lxnsy.xml.UserXML;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：白彦东
 * 创建时间：2017/1/13 17:09
 * 修改备注：
 * ================================================
 */

public class TechnologyAnswerListActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;
    private String count = "";

    private List<TechnologyVO.QuestionBean> questionVOList = new ArrayList<>();


    @Override
    public void setLayout() {
        setTitle("回答问题");
        setContentLayout(R.layout.activity_technology_answer);
    }

    @Override
    public void initMember() {
        initRecyclerView();
    }

    private void initRecyclerView() {


        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.addItemDecoration(Utils.setSpaceDecoration(this, false, true, false));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new AnswerQuestionListHolder(parent);
            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                TechnologyVO.QuestionBean questionBean = (TechnologyVO.QuestionBean) adapter.getAllData().get(position);
                TechnologyVO technologyVO = new TechnologyVO();
                TechnologyVO.PersonBeanX personBeanX = new TechnologyVO.PersonBeanX();
                personBeanX.setUserAccId(UserXML.getUserId(TechnologyAnswerListActivity.this));
                technologyVO.setQuestion(questionBean);
                technologyVO.setPerson(personBeanX);
                GotoUtil.gotoActivity(TechnologyAnswerListActivity.this, AnswerListActivity.class, "TechnologyVO", technologyVO);
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (recyclerView == null) {
                    return;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);

                switch (msg.what) {
                    case MSG.MSG_SHOW_NEW_QUESTIONS_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_SHOW_NEW_QUESTIONS_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        questionVOList = FastJsonUtil.getListBean(object.toString(), "body", "questions", TechnologyVO.QuestionBean.class);
        if (page == 1) { // 刷新
            adapter.clear();
        }

        if (questionVOList != null) {
            if (questionVOList.size() < limit) {
                adapter.stopMore();
            }
        }

        adapter.addAll(questionVOList);
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessTechnology.showNewQuestions(this, String.valueOf(page), String.valueOf(limit), count, mHandler);
    }

    @Override
    public void onLoadMore() {
            page++;
            BusinessTechnology.showNewQuestions(this, String.valueOf(page), String.valueOf(limit), count, mHandler);
    }
}
